package org.light.framework.util;

import java.math.BigDecimal;
import java.util.Date;
import java.util.Random;

import org.apache.commons.lang3.StringUtils;
import org.light.framework.enumeration.IEnum;

/**
 * 字符串处理工具类
 * 
 * @author de.cel
 *
 */
public class StringUtil extends StringUtils {

	public static boolean isNullOrEmpty(Object obj) {

		if (obj == null || obj.toString().trim().isEmpty())
			return true;
		else
			return false;
	}

	public static boolean isNotNullOrEmpty(Object obj) {
		return !isNullOrEmpty(obj);
	}

	public static int toInt(Object obj) {
		return toInt(obj, 0);
	}

	public static int toInt(Object obj, int defaultValue) {
		int value = defaultValue;
		if (obj != null) {

			String strValue = toString(obj);
			if (!isNullOrEmpty(strValue)) {
				try {
					value = Integer.parseInt(strValue);
				} catch (Exception e) {
					value = defaultValue;
				}
			}
		}
		return value;
	}

	public static String toString(Object obj) {
		return toString(obj, "");
	}

	public static String toString(Object obj, String defaultValue) {
		return obj != null ? String.valueOf(obj) : defaultValue;
	}

	public static double toDouble(Object obj) {
		return toDouble(obj, 0);
	}

	public static double toDouble(Object obj, double defaultValue) {
		double value = defaultValue;
		if (obj != null) {

			String strValue = toString(obj);
			if (!isNullOrEmpty(strValue)) {
				try {
					value = Double.parseDouble(strValue);
				} catch (Exception e) {
					value = defaultValue;
				}
			}
		}
		return value;
	}

	public static float toFloat(Object obj) {
		return toFloat(obj, 0);
	}

	public static float toFloat(Object obj, float defaultValue) {
		float value = defaultValue;
		if (obj != null) {

			String strValue = toString(obj);
			if (!isNullOrEmpty(strValue)) {
				try {
					value = Float.parseFloat(strValue);
				} catch (Exception e) {
					value = defaultValue;
				}
			}
		}
		return value;
	}

	public static BigDecimal toDecimal(Object obj) {
		BigDecimal defalueValue = new BigDecimal("0");
		return toDecimal(obj, defalueValue);
	}

	public static BigDecimal toDecimal(Object obj, BigDecimal defaultValue) {
		BigDecimal value = defaultValue;
		if (obj != null) {

			String strValue = toString(obj);
			if (!isNullOrEmpty(strValue)) {
				try {
					value = new BigDecimal(strValue);
				} catch (Exception e) {
					value = defaultValue;
				}
			}
		}
		return value;
	}

	public static long toLong(Object obj) {
		return toLong(obj, 0);
	}

	public static long toLong(Object obj, long defaultValue) {
		long value = defaultValue;
		if (obj != null) {

			String strValue = toString(obj);
			if (!isNullOrEmpty(strValue)) {
				try {
					value = Long.parseLong(strValue);
				} catch (Exception e) {
					value = defaultValue;
				}
			}
		}
		return value;
	}

	public static boolean toBoolean(Object obj) {
		return toBoolean(obj, false);
	}

	public static boolean toBoolean(Object obj, boolean defaultValue) {
		boolean value = defaultValue;
		if (obj != null) {
			value = Boolean.parseBoolean(toString(obj));
		}
		return value;
	}

	public static Date toDate(Object obj) {
		return DateHelper.stringToDate(obj);
	}

	public static String[] split(String str, String splitChar) {
		// TODO Auto-generated method stub
		return str.split("\\" + splitChar);
	}

	/**
	 * 将字符串类型转换成真实类型
	 * 
	 * @param obj
	 * @param cls
	 * @return
	 */
	public static Object convert(Object obj, Class<?> cls) {

		if (cls == String.class) {
			return toString(obj);
		} else if (cls == BigDecimal.class) {
			return toDecimal(obj);
		} else if (cls == Double.class || cls == double.class) {
			return toDouble(obj);
		} else if (cls == Float.class || cls == float.class) {
			return toFloat(obj);
		} else if (cls == Date.class) {
			return toDate(obj);
		} else if (cls == Integer.class || cls == int.class) {
			return toInt(obj);
		} else if (cls == Boolean.class || cls == boolean.class) {
			return toBoolean(obj);
		} else if (Enum.class.isAssignableFrom(cls)) {
			Object[] values = cls.getEnumConstants();
			for (Object value : values) {
				if (((IEnum) value).getId() == toInt(obj) || ((IEnum) value).getText() == toString(obj)) {
					return value;
				} else if (toString(obj) == toString(value)) {
					return value;
				}

			}
		}
		return obj;
	}
	
    /**
     * 获取一定长度的随机字符串
     * 
     * @param length
     *            指定字符串长度
     * @return 一定长度的字符串
     */
    public static String getRandomStringByLength(int length) {
        String base = "abcdefghijklmnopqrstuvwxyz0123456789";
        Random random = new Random();
        StringBuffer sb = new StringBuffer();
        for (int i = 0; i < length; i++) {
            int number = random.nextInt(base.length());
            sb.append(base.charAt(number));
        }
        return sb.toString();
    }
}
